package com.telaxo.shooter2012.bonus {
	import com.telaxo.shooter2012.Engine;
	import com.telaxo.shooter2012.gfx.Ship;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.GlowFilter;
	import flash.utils.Timer;

	

	/**
	 * @author Thomas
	 */
	public class Bonus extends Sprite {
		private var myXlimit : uint;
		private var myYlimit : uint;
		private var myTimer : Timer;
		private var target : Ship;

		public function Bonus(t : Ship) {
			target = t;
			this.graphics.beginFill(0xff0000);
			this.graphics.drawCircle(0, 0, 5);

			this.graphics.endFill();

			addEventListener(Event.ADDED_TO_STAGE, init);

			filters = [new GlowFilter(0x0000ff)];

			myTimer = new Timer(50);
			myTimer.addEventListener(TimerEvent.TIMER, onTimer);

			cacheAsBitmap = true;
		}

		private function init(e : Event) : void {
			removeEventListener(Event.ADDED_TO_STAGE, init);

			myXlimit = stage.stageWidth - 40;
			myYlimit = stage.stageHeight - 50;

			myTimer.start();

			x = 20 + Math.random() * myXlimit;
			y = 30 + Math.random() * myYlimit;
		}

		private function onTimer(e : TimerEvent) : void {
			if (hitTestObject(target)) {
				this.alpha = 0;
				target.currentWeapons+=1;
				 changePos();
				changePos();
			}

			if (!Engine.isPaused()) {
				if (alpha < 1)
					alpha += 0.07;
				scaleX = alpha;
				scaleY = alpha;
				rotation++;
			}
		}

		public function changePos() : void {
			alpha = 0;
			x = 20 + Math.random() * myXlimit;
			y = 30 + Math.random() * myYlimit;
		}
	}
}
